package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxHuopinFenlei;
import cn.stylefeng.guns.sys.modular.system.factory.LayuiTreeFactory;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxHuopinFenleiParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxHuopinFenleiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 装修货品分类表控制器
 *
 * @author liuy
 * @Date 2021-03-11 11:36:42
 */
@Controller
@RequestMapping("/dgdZxHuopinFenlei")
public class DgdZxHuopinFenleiController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdZxHuopinFenlei";

    @Autowired
    private BtyZxHuopinFenleiService btyZxHuopinFenleiService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyZxHuopinFenlei.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyZxHuopinFenlei_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyZxHuopinFenlei_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyZxHuopinFenleiParam btyZxHuopinFenleiParam) {
        BtyZxHuopinFenleiParam param = new BtyZxHuopinFenleiParam();
        param.setPid(btyZxHuopinFenleiParam.getPid());
        param.setName(btyZxHuopinFenleiParam.getName());
        List<BtyZxHuopinFenlei> list=btyZxHuopinFenleiService.list(new QueryWrapper<BtyZxHuopinFenlei>().eq("pid",btyZxHuopinFenleiParam.getPid())
        .eq("name",btyZxHuopinFenleiParam.getName()));
        if(list.size()>0){
            return ResponseData.error(1,"分类名称已存在!");
        }
        btyZxHuopinFenleiParam.setPinpai(pinpai);
        this.btyZxHuopinFenleiService.add(btyZxHuopinFenleiParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyZxHuopinFenleiParam btyZxHuopinFenleiParam) {
        List<BtyZxHuopinFenlei> list=btyZxHuopinFenleiService.list(new QueryWrapper<BtyZxHuopinFenlei>().eq("pid",btyZxHuopinFenleiParam.getPid())
                .eq("name",btyZxHuopinFenleiParam.getName()).ne("id",btyZxHuopinFenleiParam.getId()));
        if(list.size()>0){
            return ResponseData.error(1,"分类名称已存在!");
        }
        btyZxHuopinFenleiParam.setPinpai(pinpai);
        this.btyZxHuopinFenleiService.update(btyZxHuopinFenleiParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyZxHuopinFenleiParam btyZxHuopinFenleiParam) {
        this.btyZxHuopinFenleiService.delete(btyZxHuopinFenleiParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyZxHuopinFenleiParam btyZxHuopinFenleiParam) {
        BtyZxHuopinFenlei detail = this.btyZxHuopinFenleiService.getDetailById(btyZxHuopinFenleiParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-03-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyZxHuopinFenleiParam btyZxHuopinFenleiParam) {
        btyZxHuopinFenleiParam.setPinpai(pinpai);
        return this.btyZxHuopinFenleiService.findPageBySpec(btyZxHuopinFenleiParam);
    }

    @RequestMapping("/layuiTree")
    @ResponseBody
    public List<LayuiTreeNode> layuiTree(){
        List<LayuiTreeNode> list = this.btyZxHuopinFenleiService.layuiTreeDgd();
        list.add(LayuiTreeFactory.createRoot());
        DefaultTreeBuildFactory<LayuiTreeNode> treeBuildFactory = new DefaultTreeBuildFactory<>();
        treeBuildFactory.setRootParentId("-1");
        return treeBuildFactory.doTreeBuild(list);
    }

    /**
     * 获取kemu的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2019-8-27 15:24
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.btyZxHuopinFenleiService.treeDgd();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    @ResponseBody
    @RequestMapping("/getHpFlList")
    public ResponseData getHpFlList(BtyZxHuopinFenleiParam huopinFenleiParam) {
        return ResponseData.success(this.btyZxHuopinFenleiService.getHpFlList(huopinFenleiParam));
    }

}


